package com.gopay.bis.web.controller.ebank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcB2cMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;

@Controller
@Scope("prototype")
public class CmbcB2cServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(CmbcB2cServerRes.class);

	@RequestMapping(value = "/cmbcB2cServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- CmbcB2cServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		CmbcB2cMessage message = new CmbcB2cMessage();
		message.setBank(BankConstants.BANK_LOGO_CMBC);
		String signText = request.getParameter("payresult");
		message.setSignMessage(signText);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        // 银行接口通道信息 00: 直连
        message.setBankIntrChann(BisBankInterfaceChannel._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		CmbcMessage message = (CmbcMessage) result.getModel("res");
		if (CmbcConstants.ORDER_STATE_SUCCESS.equals(message.getOrderStatus())) {
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(message.getAmount().toString());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg(request.getParameter("resMsg"));
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("payresult"))) {
		return false;
		}
		return true;
	}

}
